package handle

import (
	"strconv"
	"time"
	"user-hubs/cache"
	"user-hubs/model"
	"user-hubs/pkg"

	"github.com/gin-gonic/gin"
	"github.com/goccy/go-json"
)

func User(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	cache_Key := "pass" + username
	cacheCount, _ := cache.Rdb.Get(cache.Ctx, cache_Key).Int()
	if cacheCount >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误3次",
		})
		return
	}
	var user model.User
	model.DB.Where("username=?", username).Find(&user)
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	if pkg.Md5(password) != user.Password {
		cache.Rdb.Incr(cache.Ctx, cache_Key)
		if cacheCount == 2 {
			cache.Rdb.Set(cache.Ctx, cache_Key, "3", time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误封禁2小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
	return

}
func UserRole(c *gin.Context) {
	id := c.Query("id")
	var role model.Role
	cache_Key := "rid" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, cache_Key).Result()
	if cacheData == "" {
		/*
				SELECT * FROM
			roles LEFT JOIN user_roles ON user_roles.rid = roles.id WHERE user_roles.uid=1
		*/
		model.DB.Table("roles").
			Joins("roles LEFT JOIN user_roles ON user_roles.rid = roles.id").
			Where("uid=?", id).First(&role)
		json, _ := json.Marshal(role)
		cache.Rdb.Set(cache.Ctx, cache_Key, json, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &role)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": role,
	})
	return
}
func RoleNode(c *gin.Context) {
	rid := c.Query("rid")
	var node model.Node

	err := model.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes on role_nodes.nid = nodes.id ").
		Where("role_nodes.rid=?", rid).First(&node).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": node,
	})
	return

}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var count int64
	model.DB.Model(&model.Userm{}).Count(&count)
	var user []model.Userm
	cache_Key := "page" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, cache_Key).Result()
	if cacheData == "" {
		model.DB.Offset(offset).Limit(size).Find(&user)
		json, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, cache_Key, json, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  user,
		"count": count,
	})
	return
}
func Login(c *gin.Context) {
	id := c.Query("id")
	var user []model.Userm
	cache_Key := "pag" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, cache_Key).Result()
	if cacheData == "" {
		model.DB.Where("id=?", id).Find(&user)
		json, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, cache_Key, json, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": user,
	})
	return
}
